<?php

namespace Acme\StaffBundle\Form\EventListener;

use Symfony\Component\Form\Event\FilterDataEvent;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvents;

class EditCategoryEventListener implements EventSubscriberInterface {

  private $factory;

  public function __construct(FormFactoryInterface $factory) {
    $this->factory = $factory;
  }

  public static function getSubscribedEvents() {

    return array( FormEvents::BIND_CLIENT_DATA => 'bindClientData' );
  }

  public function bindClientData(FilterDataEvent $event) {
    $data = $event->getData();
    $object = $event->getForm()->getData();

    if ( null === $data ) {
      return;
    }
    if ( !isset($data['category']) && $object->getCategory() ) {
      $data['category'] = $object->getCategory()->getId();
      $event->setData($data);
    }
  }

}

?>
